﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Redis.Utils
{
    /// <summary>
    /// 可排序的集合
    /// </summary>
    public partial class RedisClient_Hash : RedisClient
    {
        #region 增加元素
        private bool _Add(string key, byte[] member)
        {
            if (!CheckKey(key))
            {
                return false;
            }
            return redisSocket.SendDataExpectInt(member, "hmset {0} {1} \r\n", key,  member.Length) > 0 ? true : false;
        }
        /// <summary>
        /// 增加元素到SETS序列,如果元素（member）不存在则添加成功 1，否则失败 0;
        /// </summary>
        /// <param name="key"></param>
        /// <param name="member"></param>
        /// <returns></returns>
        public bool Add(string key, object member)
        {
            return _Add(key, Serialize(member));
        }
        #endregion 增加元素

        #region 获取数据
        byte[] _GetList(string key)
        {
            if (!CheckKey(key))
            {
                return null;
            }
            return redisSocket.SendExpectData(null, "hmget  {0} {1} \r\n", key);
        }


        public object GetList(string key)
        {
            byte[] data = _GetList(key);
            if (data == null || data.Length == 0)
            {
                return null;
            }
            return Deserialize(data);
        }
        #endregion

        #region 删除集合中的元素
        bool _Remove(string key, byte[] member)
        {
            if (!CheckKey(key))
            {
                return false;
            }
            return redisSocket.SendDataExpectInt(member, "hdel  {0} {1}\r\n", key, member.Length) > 0 ? true : false;
        }
        public bool Remove(string key, object member)
        {
            return this._Remove(key, Serialize(member));
        }
        #endregion


    }
}

